package cn.zzu.Etrip.twoday.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.zzu.Etrip.twoday.activity.TwoDayPayActivity;
import cn.zzu.Etrip.twoday.service.lightAndPrice;
import com.example.administrator.etrip.R;

import java.util.ArrayList;
import java.util.List;

import cn.zzu.Etrip.twoday.service.lightAndPrice;

/**
 * Created by ASUS on 2018/5/24.
 */

public class ContentFragment extends Fragment{

    private ImageView imageView,imageView1,imageView2,imageView3,imageView4,imageView5;
    private TextView textView,textView1,textView2,textView3,textView4,textView5;
    private TextView priceText,priceText1,priceText2,priceText3,priceText4,priceText5;
    public List<ImageView>imageViews;
    public List<TextView> textViews;
    public List<TextView> textPrices;
    public String []image;
    public String []price;
    public Integer [] address;
    //声明Contentfragment内部的组件的名字，供调用参数或方法。
   lightAndPrice lightAndPrice = new lightAndPrice();
    public static ContentFragment returnContentFragment(String []i, String []p, Integer[] a){
        ContentFragment fragment = new ContentFragment();
        fragment.image = i;
        fragment.price = p;
        fragment.address = a;
        return fragment;
    }


    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.two_content_fragment,container,false);
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       findViewAndSetListener(view);
    }
    public void findViewAndSetListener(View view){
        imageViews = new ArrayList<ImageView>();
        //查找ImageView的图像
        imageView =  view.findViewById(R.id.two_zhengzhou_image1);
        imageView1 =  view.findViewById(R.id.two_zhengzhou_image2);
        imageView2 =  view.findViewById(R.id.two_zhengzhou_image3);
        imageView3 =  view.findViewById(R.id.two_zhengzhou_image4);
        imageView4 =  view.findViewById(R.id.two_zhengzhou_image5);
        imageView5 = view.findViewById(R.id.two_zhengzhou_image6);
        imageViews.add(imageView);
        imageViews.add(imageView1);
        imageViews.add(imageView2);
        imageViews.add(imageView3);
        imageViews.add(imageView4);
        imageViews.add(imageView5);

       //查找content的TextView对象
        textViews = new ArrayList<TextView>();
        textView = view.findViewById(R.id.two_zhenzhou_contenttext1);
        textView1 = view.findViewById(R.id.two_zhenzhou_contenttext2);
        textView2= view.findViewById(R.id.two_zhenzhou_contenttext3);
        textView3 = view.findViewById(R.id.two_zhenzhou_contenttext4);
        textView4 = view.findViewById(R.id.two_zhenzhou_contenttext5);
        textView5 = view.findViewById(R.id.two_zhenzhou_contenttext6);
        textViews.add(textView);
        textViews.add(textView1);
        textViews.add(textView2);
        textViews.add(textView3);
        textViews.add(textView4);
        textViews.add(textView5);
        //查找price的对象
        textPrices = new ArrayList<TextView>();
        priceText = view.findViewById(R.id.two_zhenzhou_price1);
        priceText1 = view.findViewById(R.id.two_zhenzhou_price2);
        priceText2= view.findViewById(R.id.two_zhenzhou_price3);
        priceText3 = view.findViewById(R.id.two_zhenzhou_price4);
        priceText4 = view.findViewById(R.id.two_zhenzhou_price5);
        priceText5 = view.findViewById(R.id.two_zhenzhou_price6);
        textPrices.add(priceText);
        textPrices.add(priceText1);
        textPrices.add(priceText2);
        textPrices.add(priceText3);
        textPrices.add(priceText4);
        textPrices.add(priceText5);
        //给各个组件天加内容。
        setImagetAndSetText(imageViews,textViews,textPrices,image,price,address);
    }

    public void setImagetAndSetText(List<ImageView> imageViews,List<TextView> textViews,List<TextView>textPrices,String [] image,String[] price,Integer [] address){
        //定义一个用来传id的值
        final Integer []id = address;


        if(price==null||image==null||address==null||imageViews==null||textViews==null){
            lightAndPrice iLAndP = new lightAndPrice();
            //动态给Fragment里面的组件进行赋值操作
            for(int a =0;a<imageViews.size();a++){
                imageViews.get(a).setImageResource(iLAndP.zhengzhou_address[a]);
                textViews.get(a).setText(iLAndP.zhengzhou_cotent[a]);
                textPrices.get(a).setText(iLAndP.zhengzhuo_price[a]);
                imageViews.get(a).setOnClickListener(new Listener(iLAndP.zhengzhou_address[a],iLAndP.zhengzhou_address[a].toString(),iLAndP.zhengzhuo_price[a].toString()));
                textViews.get(a).setOnClickListener(new Listener(iLAndP.zhengzhou_address[a],iLAndP.zhengzhou_address[a].toString(),iLAndP.zhengzhuo_price[a].toString()));
            }
          //当动态的调用数据时，给fragment里面的组件进行赋值
        }else {
            for(int a =0;a<image.length;a++){
                imageViews.get(a).setImageResource(address[a]);
                textViews.get(a).setText(image[a]);
                textPrices.get(a).setText(price[a]);
                ///设置监听器
                imageViews.get(a).setOnClickListener(new Listener(address[a],address[a].toString(),price[a]));
                textViews.get(a).setOnClickListener(new Listener(address[a],address[a].toString(),price[a]));
            }
        }
    }

    ///创建内部类
    class Listener implements View.OnClickListener {
        Integer id;
        String k;
        String p;
        public Listener( Integer id, String k,String p){
            this.id = id;
            this.k = k;
            this.p = p;
        }
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.twoDayPay");
            Bundle bundle = new Bundle();
            bundle.putInt("key",id);
            bundle.putString("text",k);
            bundle.putString("price",p);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }


}

